package com.baizhi.controller;

import com.baizhi.annotation.LogRecord;
import com.baizhi.entity.BzAdmin;
import com.baizhi.enums.LogTypeEnum;
import com.baizhi.service.BzAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yu
 */
@Controller
public class BzAdminController {
    @Autowired
    private BzAdminService bzAdminService;
    @LogRecord(type = LogTypeEnum.LOGIN,content = "用户执行登陆操作")
    @RequestMapping("/bzAdmins/login")
    @ResponseBody
    public Map login(String username, String password, String validateCode, HttpSession session){
        HashMap<String, Object> map = new HashMap<>();
        String trueValidateCode = (String)session.getAttribute("trueValidateCode");
        if(!trueValidateCode.equals(validateCode)){
            map.put("status","fail");
            map.put("reason","验证码错误！");
        }else {
            BzAdmin bzAdmin = bzAdminService.login(username, password);
            if(bzAdmin==null){
                map.put("status","fail");
                map.put("reason","用户名或密码错误");
            }else{
                map.put("status","success");
                map.put("user",bzAdmin);
                session.setAttribute("user",bzAdmin);
            }
        }
        return map;
    }
    @LogRecord(type = LogTypeEnum.SELECT,content = "查询全部管理员")
    @ResponseBody
    @RequestMapping("/bzAdmins/showAll")
    public Map showAll(Integer page,Integer limit){
        Map map = bzAdminService.showAll(page, limit);
        return map;
    }
    @LogRecord(type = LogTypeEnum.SELECT,content = "查询单个管理员")
    @GetMapping("/bzAdmins/{id}")
    @ResponseBody
    public Map showOne(@PathVariable("id")Integer id){
        HashMap<String, Object> map = new HashMap<>();
        BzAdmin bzAdmin = bzAdminService.showOne(id);
        map.put("status","success");
        map.put("data",bzAdmin);
        return map;
    }
    @LogRecord(type = LogTypeEnum.INSERT,content = "添加管理员")
    @PostMapping("/bzAdmins")
    @ResponseBody
    public Map insert(@RequestBody BzAdmin bzAdmin){
        HashMap<String, Object> map = new HashMap<>();
        bzAdminService.insert(bzAdmin);
        map.put("status","success");
        return map;
    }
    @LogRecord(type = LogTypeEnum.DELETE,content = "删除管理员")
    @DeleteMapping("/bzAdmins/{id}")
    @ResponseBody
    public Map delete(@PathVariable("id")Integer id){
        HashMap<String, Object> map = new HashMap<>();
        bzAdminService.delete(id);
        map.put("status","success");
        return map;
    }
    @LogRecord(type = LogTypeEnum.UPDATE,content = "添加管理员")
    @PutMapping("/bzAdmins")
    @ResponseBody
    public Map update(@RequestBody BzAdmin bzAdmin){
        HashMap<String, Object> map = new HashMap<>();
        bzAdminService.update(bzAdmin);
        map.put("status","success");
        return map;
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "redirect:/jsp/product/main.jsp";
    }
}
